const Uniq = require("../template")

function testUniqBasic() {
  console.log(
    "1. 基本去重功能测试:",
    (() => {
      const array = [1, 2, 2, 3, 3, 3, 4, 5, 5]
      const result = Uniq.uniq(array)
      const expected = [1, 2, 3, 4, 5]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "2. 字符串数组去重测试:",
    (() => {
      const array = ['a', 'b', 'b', 'c', 'c', 'c', 'd', 'e', 'e']
      const result = Uniq.uniq(array)
      const expected = ['a', 'b', 'c', 'd', 'e']
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "3. 对象数组去重测试:",
    (() => {
      const array = [
        { id: 1, name: 'Alice' },
        { id: 2, name: 'Bob' },
        { id: 1, name: 'Alice' },
        { id: 3, name: 'Charlie' }
      ]
      const result = Uniq.uniq(array)
      return result.length === 4 // 对象引用不同，不会去重
    })()
  )

  console.log(
    "4. 自定义比较函数去重测试:",
    (() => {
      const array = [
        { id: 1, name: 'Alice' },
        { id: 2, name: 'Bob' },
        { id: 1, name: 'Alice' },
        { id: 3, name: 'Charlie' }
      ]
      const result = Uniq.uniqBy(array, 'id')
      return result.length === 3 // 根据id去重
    })()
  )

  console.log(
    "5. 条件去重测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = Uniq.uniqWith(array, (a, b) => a % 2 === b % 2)
      return result.length === 2 // 奇数和偶数各一个
    })()
  )

  console.log(
    "6. 深度去重测试:",
    (() => {
      const array = [
        [1, 2, 3],
        [1, 2, 3],
        [4, 5, 6],
        [1, 2, 3]
      ]
      const result = Uniq.uniqDeep(array, 1)
      return result.length === 2 // 深度去重后应该只有2个
    })()
  )

  console.log(
    "7. 排序去重测试:",
    (() => {
      const array = [3, 1, 2, 2, 3, 1, 4, 5, 5]
      const result = Uniq.uniqSorted(array, (a, b) => a - b)
      const expected = [1, 2, 3, 4, 5]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "8. 类型去重测试:",
    (() => {
      const array = [1, '1', 2, '2', 3, '3', 4, '4']
      const result = Uniq.uniqByType(array, 'number')
      return result.length === 4 // 只保留数字类型
    })()
  )

  console.log(
    "9. 转换去重测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = Uniq.uniqWithTransform(array, x => x % 3)
      return result.length === 3 // 余数0,1,2各一个
    })()
  )

  console.log(
    "10. 验证去重测试:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = Uniq.uniqWithValidation(array, { min: 1, max: 10 })
      return result.length === 10 // 所有元素都有效
    })()
  )

  console.log(
    "11. 空数组去重测试:",
    (() => {
      const array = []
      const result = Uniq.uniq(array)
      return result.length === 0
    })()
  )

  console.log(
    "12. 单元素数组去重测试:",
    (() => {
      const array = [1]
      const result = Uniq.uniq(array)
      return result.length === 1 && result[0] === 1
    })()
  )

  console.log(
    "13. this上下文去重测试:",
    (() => {
      const uniqObj = {
        multiplier: 2,
        processUniq: function(array) {
          const result = Uniq.uniq(array)
          return result.length * this.multiplier
        }
      }
      const array = [1, 2, 2, 3]
      return uniqObj.processUniq(array) === 6 // 3*2=6
    })()
  )

  console.log(
    "14. 复杂对象this绑定去重测试:",
    (() => {
      const uniqConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(array) {
          const result = Uniq.uniq(array)
          return result.length >= this.config.threshold
        }
      }
      const array = [1, 2, 2, 3]
      return uniqConfigObj.processWithConfig(array) === true
    })()
  )
}

testUniqBasic()
